package com.smarthospital.service.impl;

import com.smarthospital.common.Result.CodeMsg;
import com.smarthospital.common.Result.CommonException;
import com.smarthospital.common.util.SHDateUtil;
import com.smarthospital.contants.SHConst;
import com.smarthospital.dao.daoService.InquiryOrderDaoService;
import com.smarthospital.entity.dto.TrtcUserSigDTO;
import com.smarthospital.entity.pojo.InquiryOrder;
import com.smarthospital.entity.pojo.User;
import com.smarthospital.service.IMControlService;
import com.smarthospital.web.inteceptor.LoginInterceptor;
import com.tencentyun.TLSSigAPIv2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * create by sym  2021/7/17 12:24.
 */
@Slf4j
@Service
public class IMControlServiceImpl implements IMControlService {


    @Autowired
    InquiryOrderDaoService orderDaoService;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    TLSSigAPIv2 tLSSigAPIv2;


    @Override
    public void startIM(Integer type,String orderId) {
        User user = LoginInterceptor.getUser();
        InquiryOrder order = orderDaoService.queryById(Long.valueOf(orderId));
        if (null == order) {
            throw new CommonException(CodeMsg.ERROR_NO_ORDER);
        }
        if (!user.getId().equals(order.getInquiryManId()) && !user.getId().equals(order.getDoctorId())) {
            throw new CommonException(CodeMsg.ERROR_NO_RIGHT);
        }
        redisTemplate.opsForValue().set(String.format(SHConst.redis_key.startIM,orderId),System.currentTimeMillis()+"" );
    }

    @Override
    public void closeIM(Integer type, String orderId) {
        User user = LoginInterceptor.getUser();
        InquiryOrder order = orderDaoService.queryById(Long.valueOf(orderId));
        if (null == order) {
            throw new CommonException(CodeMsg.ERROR_NO_ORDER);
        }
        if (!user.getId().equals(order.getInquiryManId()) && !user.getId().equals(order.getDoctorId())) {
            throw new CommonException(CodeMsg.ERROR_NO_RIGHT);
        }
        //获取缓存信息
        String cacheStr = redisTemplate.opsForValue().get(String.format(SHConst.redis_key.startIM, orderId));
        if (StringUtils.isEmpty(cacheStr)) {
            throw new CommonException(CodeMsg.ERROR_NO_STARTED);
        }

        long now = System.currentTimeMillis();
        Long startTime = Long.valueOf(cacheStr);
        int time = (int)(now - startTime)/1000*60;

        //更新时间
        order.setLeftVedioTime(order.getLeftVedioTime()-time);
        orderDaoService.update(order);
    }

    @Override
    public void checkIM(Integer type, String orderId) {
        User user = LoginInterceptor.getUser();
        InquiryOrder order = orderDaoService.queryById(Long.valueOf(orderId));
        if (null == order) {
            throw new CommonException(CodeMsg.ERROR_NO_ORDER);
        }
        if (!user.getId().equals(order.getInquiryManId()) && !user.getId().equals(order.getDoctorId())) {
            throw new CommonException(CodeMsg.ERROR_NO_RIGHT);
        }
        if (order.getLeftVedioTime() <=0) {
            throw new CommonException(CodeMsg.ERROR_NO_LEFT_TIME);
        }
        if (order.getStatus() !=1) {
            throw new CommonException(CodeMsg.ERROR_ORDER_OVER);
        }
        if (!order.getInquiryTime().equals(SHDateUtil.getRangeTime())) {
            throw new CommonException(CodeMsg.ERROR_RANGE_TIME);
        }
    }

    @Override
    public TrtcUserSigDTO initRoom(String orderId) {
        User user = LoginInterceptor.getUser();
        InquiryOrder order = orderDaoService.queryById(Long.valueOf(orderId));
        if (order.getInquiryType() != 2 &&order.getInquiryType() != 1) {
            throw new CommonException(CodeMsg.ERROR_NOT_IM);
        }
        String userId = order.getRoomId() +"_"+ user.getId();
        String userSig = tLSSigAPIv2.genUserSig(userId, 86400);

        return TrtcUserSigDTO.builder().userID(userId).UserSig(userSig).build();
    }


}
